package com.example.service.impl;

import com.example.mapper.ApartmentMapper;
import com.example.mapper.EvaluateMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Apartment;
import com.example.pojo.Evaluate;
import com.example.pojo.User;
import com.example.service.EvaluateService;
import com.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EvaluateServiceImpl implements EvaluateService {
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private ApartmentMapper apartmentMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addEvaluate(Evaluate evaluate, HttpServletRequest request) {
        Integer idU = JwtUtils.parseJWT(request.getHeader("token")).get("id", Integer.class);
        Apartment apartmentId = apartmentMapper.getApartmentId(evaluate.getIdA());
        User pim = userMapper.getPim(idU);
        evaluate.setName(apartmentId.getName());
        evaluate.setFimage(apartmentId.getImage());
        evaluate.setTname(pim.getNickname());
        evaluate.setTimage(pim.getImage());
        evaluate.setDate(LocalDateTime.now());
        evaluate.setLocation(apartmentId.getLocation());
        evaluate.setIdF(apartmentId.getIdU());
        evaluate.setIdU(idU);
        evaluateMapper.addEvaluate(evaluate);
    }

    @Override
    public List<Evaluate> getEvaluateTenant(HttpServletRequest request) {
        List<Evaluate> list=evaluateMapper.getEvaluateTenant(JwtUtils.parseJWT(request.getHeader("token")).get("id",Integer.class));
        return list;
    }

    @Override
    public List<Evaluate> getEvaluateLandlady(HttpServletRequest request) {
        List<Evaluate> list=evaluateMapper.getEvaluateLandlady(JwtUtils.parseJWT(request.getHeader("token")).get("id",Integer.class));
        return list;
    }

    @Override
    public List<Evaluate> getEvaluateId(Integer id) {
        List<Evaluate> list=evaluateMapper.getEvaluateId(id);
        return list;
    }
}
